#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <sys/socket.h>

int get_listen_fd();

int main(int argc, char *argv[])
{
    int tcp_fd = get_listen_fd();

    int epoll_fd = epoll_create(1);
    struct epoll_event tcp_event;
    tcp_event.data.fd = tcp_fd;
    tcp_event.events = EPOLLIN | EPOLLOUT | EPOLLHUP;

    epoll_ctl(epoll_fd, EPOLL_CTL_ADD, tcp_fd, &tcp_event);
    
    struct epoll_event ctl_event[1000];
    int con_fd;
    int sock_fd;
    int number;

    struct epoll_event sock_event;


    while(1)
    {
        number = epoll_wait(epoll_fd,ctl_event,1000,-1);
        if(number < 0)
        {
            perror("epoll_wait fail");
            exit(-1);
        }
        else if(number > 0)
        {
            for(int i = 0;i <number;i++)
            {

            }
        }
    }

    close(tcp_fd);
    return 0;
}

int get_listen_fd()
{
    int tcp_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (tcp_fd < 0)
    {
        perror("socket fail");
        exit(-1);
    }

    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(50000);
    inet_pton(AF_INET, "0.0.0.0", &server_addr.sin_addr);

    int ret = bind(tcp_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
    if (ret < 0)
    {
        perror("bind fail");
        exit(-1);
    }

    listen(tcp_fd, 5);

    return tcp_fd;
}
